<?php

/**
 *
 * @author Administrator
 */
class VideosController {
    // TODO - Insert your code here
    private $path;
    private $lan;

    /**
     */
    function __construct() {

        $this->path = "../src/module/admin/view_videos/temp/";

        if (isset( $_SESSION[SESSION_LANGUAGE])) {
        	$this->lan = $_SESSION[SESSION_LANGUAGE];
        } else {
        	$this->lan = DEFAULT_LANGAUGE;
        }
    }

    /**
     * View gird page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewGrid() {

        $template = new XTemplate( $this->path . $this->lan . "/grid.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_VIDEOS);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_DELETE_MANY);
        
        $db = new DBAccess();
        $list = $db->selectManyByCause($GLOBALS['videoShortColArray'], VIDEOS, array(VIDEO_LANGUAGE => $this->lan), array(VIDEO_UPDATETIME => DESC));
        
        
        // assign list
        foreach ($list as $item) {
            $item[VIDEO_VIDEOLINK] = str_replace("http://youtu.be/", "", $item[VIDEO_VIDEOLINK]);
            $template->assign( ITEM, $item);
            $template->parse( PAGECONTENT .".".ITEM);
        }
        
        
        // assign gird title
        $template->assign( GRIDTITLE, "Videos - Clip");
        
        // assign item count and page count
        $template->assign( TOTALCOUNT, count($list));
        $template->assign( TOTALPAGE, 1);
        
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * View sort page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewSort() {

        $template = new XTemplate( $this->path . $this->lan . "/sort.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_VIDEOS);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_SORT);
        $template->assign( TYPE, $_REQUEST[PARAM_FID]);
        
        $db = new DBAccess();
        $list = $db->selectManyByCause($GLOBALS['imageShortColArray'], IMAGES, array(IMAGE_AID => $_REQUEST[PARAM_FID]), array(IMAGE_POSITION => ASC));
        
        
        // assign list
        foreach ($list as $item) {
            $template->assign( ITEM, $item);
            $template->parse( PAGECONTENT .".".ITEM);
        }
        
        // assign item count and page count
        $template->assign( TOTALCOUNT, count($list));
        $template->assign( TOTALPAGE, 1);
        
        
        // assign gird title
        $template->assign( SORTTITLE, "Videos - Clip");
        
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * View form page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewForm() {

        $template = new XTemplate( $this->path . $this->lan . "/form.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_VIDEOS);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_FORM);
        
        // get id
        if (isset($_REQUEST[PARAM_ID]) && !empty($_REQUEST[PARAM_ID])){
            $db = new DBAccess();
            $item = $db->selectOneByCause($GLOBALS['videoFullColArray'], VIDEOS, array(VIDEO_ID => $_REQUEST[PARAM_ID]));
        } else {
            $item = array();
            $item[VIDEO_VIDEOLINK] = "view";
        }
        
        $item["view"] = str_replace("http://youtu.be/", "", $item[VIDEO_VIDEOLINK]);
        
        
        // assign gird title
        $db = new DBAccess();
        $template->assign( FORMTITLE, "Videos - Clip");
        
        // assign form item
        $template->assign( FORMITEM, $item);
        $template->parse( PAGECONTENT .".".FORMITEM);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * submit form function
     * @return multitype:
     */
    public function submitForm() {
        
        // reponse array
        $response_array = array();
        
        // get input
        $item = array();
        $item[IMAGE_ID] = $_REQUEST[IMAGE_ID];
        
        $item[VIDEO_TITLE] = $_REQUEST[VIDEO_TITLE];
        $item[VIDEO_DESCRIPTION] = $_REQUEST[VIDEO_DESCRIPTION];
        $item[VIDEO_VIDEOLINK] = $_REQUEST[VIDEO_VIDEOLINK];
        
        
        
        $item[VIDEO_TITLETAG] = $_REQUEST[VIDEO_TITLETAG];
        $item[VIDEO_URL] = encodeURL( $_REQUEST[VIDEO_URL]);
        $item[VIDEO_METAKEYWORD] = $_REQUEST[VIDEO_METAKEYWORD];
        $item[VIDEO_METADESCRIPTION] = $_REQUEST[VIDEO_METADESCRIPTION];
        
        $item[VIDEO_UPDATETIME] = getCurrentDateTime();
        $item[VIDEO_LANGUAGE] = $this->lan;
        $item[VIDEO_EDITBY] = $_SESSION[SESSION_USERNAME];
        
        // update data to database
        $db = new DBAccess();
        
        if ($item[VIDEO_ID] == 0) {
            if ($db->insertData( $item, VIDEOS)) {
                $new = $db->selectOneByCause( array(VIDEO_ID, VIDEO_URL), VIDEOS, array(VIDEO_URL => $item[VIDEO_URL]));
                updateDataType(VIDEOS, $new[VIDEO_ID], $new[VIDEO_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_VIDEOS . "&act=" . PVALUE_ACTION_VIEW_GRID;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_FAIL);
            }
        } else {
            if ($db->updateData( $item, VIDEOS)) {
                updateDataType(VIDEOS, $item[VIDEO_ID], $item[VIDEO_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_VIDEOS . "&act=" . PVALUE_ACTION_VIEW_GRID;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_FAIL);
            }
        }
        // return repnose array;
        return $response_array;
    }

    /**
     * submit form function
     * @return multitype:
     */
    public function submitSort() {
        
        // response array
        $response_array = array();
        
        // get input
        $array = $_POST[PARAM_LIST];
		
		$db = new DBAccess();
		$position = 1;
		
		// update position
		foreach ( $array as $key => $value ) {
			$value = ( int ) $value;
			if ($value == 0) {
				$caterory = array(IMAGE_ID => $key, IMAGE_POSITION  => $position);
				$position ++;
				$db->updateData( $caterory, IMAGES);
			}
		}
		
		$response_array[STATUS] = STATUS_OK;
		$response_array[MESSAGE] = MESSAGE_UPDATE_SUCCESSFUL;
		
		// return response  array
		return $response_array;
    }

    /**
     * submit delete one function
     * @return multitype:
     */
    public function submitDeleteOne() {
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
            
            $db = new DBAccess();
            
            if ($db->deleteData( VIDEOS, $_REQUEST[PARAM_ID])) {
                deleteDataType(VIDEOS, $_REQUEST[PARAM_ID]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
            }
        } else {
            $response_array[STATUS] = STATUS_FAIL;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }

    /**
     * submit delete many function
     * @return multitype:
     */
    public function submitDeleteMany() {
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_SELECTED])) {
            
            $IDArray = $_REQUEST[PARAM_SELECTED];
            
            if ($IDArray != null && count( $IDArray) > 0) {
                $delete = true;
                $db = new DBAccess();
                foreach ( $IDArray as $id ) {
                    if ($db->deleteData( VIDEOS, $id)) {
                        deleteDataType(VIDEOS, $id);
                    } else {
                        $delete = false;
                    }
                }
                
                if ($delete) {
                    $response_array[STATUS] = STATUS_OK;
                    $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
                } else {
                    $response_array[STATUS] = STATUS_FAIL;
                    $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
                }
            } else {
                $response_array[STATUS] = STATUS_ERROR;
                $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
            }
        } else {
            $response_array[STATUS] = STATUS_ERROR;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }

    /**
     */
    function __destruct() {
        
        // TODO - Insert your code here
    }
}

?>